#include "bullet.h"
#include <QDebug>

Bullet::Bullet(QPointF start, qreal a,Player* o): MovingObject()
{
	owner = o;
    isFirstUpdate = true;
    velocity = 30;
    distanceTravelled = velocity;
    angle = a;
    start_pos = start;
    setPos(start_pos);
    bulletLen = 40;
    halfLen = bulletLen/2;
    isHit = false;

    if (bulletLen > velocity) actualBulletLen = velocity;
    else actualBulletLen = bulletLen;

    QLineF line(0,0,velocity,0);
    line.setAngle(angle);
    double dx = line.dx();
    double dy = line.dy();

    if (dx < 0 && dy < 0)
    {
        bulletRect.setRect(dx,dy,-dx,-dy);
    }
    else if (dx < 0 && dy >= 0)
    {
        bulletRect.setRect(dx,0,-dx,dy);
    }
    else if (dx >= 0 && dy < 0)
    {
        bulletRect.setRect(0,dy,dx,-dy);
    }
    else
    {
        bulletRect.setRect(0,0,dx,dy);
    }
    shapeLine = line;
    drawLine = QLineF(line.p2(), line.p1());
    drawLine.setLength(actualBulletLen);

    bulletShape.addPolygon(QPolygonF() << line.p1() << line.p2());
    setZValue(1);
}

void Bullet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                    QWidget *widget)
{
	Collision coll;
	QList<QGraphicsPolygonItem*> polys = coll.getCollidingPolygons(this);
	if (polys.count() > 0)
	{
		QPointF *iPoint = new QPointF();
		QPointF p1 = mapToScene(shapeLine.p1());
		QPointF p2 = mapToScene(shapeLine.p2());
		double len = coll.getClosestIntersectionPointOnPolygons(QLineF(p1,p2),polys, iPoint);

		if (len > 0)
		{
			shapeLine.setLength(len);
			if ((bulletLen >= velocity)||(len >= bulletLen) )
			{
				drawLine.setP1(shapeLine.p2());
			}
			else
			{
				drawLine.setLength(0);
			}
		}
	}

    painter->drawLine(drawLine);
    /*painter->drawEllipse(shapeLine.p1(),6,6);
    painter->drawEllipse(shapeLine.p2(),3,3);
    painter->drawRect(bulletRect);*/
}

void Bullet::checkCollisions()
{
	Collision coll;

	QList<Player*> players = coll.getCollidingPlayers(this);
	foreach (Player* p, players)
	{
		if (p == owner) continue;
		else
		{
			qDebug() << "emit hit player";
			emit hitPlayer(owner,p);

			isHit = true;
		}
	}


	QList<QGraphicsPolygonItem*> polys = coll.getCollidingPolygons(this);
	//if (owner->getIsRemote()) qDebug() << polys.count();
	if (polys.count() > 0)
	{
		isHit = true;
	}
}

QRectF Bullet::boundingRect() const
{

    return bulletRect;
}
QPainterPath Bullet::shape () const
{

    return bulletShape;
}

void Bullet::moveBullet()
{
	checkCollisions();

    if (!isFirstUpdate)
    {

        QLineF line(0,0,velocity,0);;
        line.setAngle(angle);
        
        if (isHit)
        {
            map()->removeItem(this);
            emit hitPolygon();
        }
        isFirstUpdate = false;
        moveBy(line.dx(),line.dy());
        if (distanceTravelled < bulletLen)
        {
            distanceTravelled += velocity;
            actualBulletLen += velocity;
            drawLine.setLength(actualBulletLen);
            if (distanceTravelled >= bulletLen)
            {
                actualBulletLen = bulletLen;
                drawLine.setLength(actualBulletLen);
            }
        }
    } else {
        isFirstUpdate = false;
    }

}

Map *Bullet::map()
{
    return static_cast<Map *>(scene());
}

